<?php

namespace addons\kzvote\controller;

use addons\kzvote\library\WechatService;
use app\common\model\kzvote\gift\Gift as GiftModel;
use app\common\model\kzvote\gift\Order;
use think\Db;
use think\Exception;

class Gift extends Base
{
    protected $noNeedLogin = ['index'];

    /**
     * 查询出所有礼物
     */
    public function index()
    {
        $where = [];
        if ($this->platform == 'wechat'){
            $where['money'] = ['>',0];
        }
        $list = GiftModel::where($where)->select();
        if (count($list) > 3){
            $arr[0] = $arr[1] = [];
            foreach ($list as $key=>$row){
                if ($key % 2 == 0){
                    array_push($arr[0],$row);
                }else{
                    array_push($arr[1],$row);
                }
            }
        }else{
            $arr[0] = $list;
        }
        $this->success('',$arr);
    }

    /**
     * 看视频奖票数
     */
    public function submit(){
        $user = $this->auth->getUser();
        $vote_id   = $this->request->param('vote_id');
        $person_id = $this->request->param('person_id');
        $gift_id   = $this->request->param('gift_id');
        try {
            $gift = GiftModel::get($gift_id);
            if ($gift['money'] > 0){
                $this->error('操作异常');
            }
            $vote = \app\common\model\kzvote\Vote::get($vote_id);
            if ($vote['status'] != '10' || $vote['check'] != '1' || $vote['start_time'] > time()) {
                $this->error('当前活动不允许送礼物');
            }

            if ($vote->end_time < time()) {
                $this->error('投票活动已结束');
            }

            Order::create([
                'admin_id'  => $vote['admin_id'],
                'user_id'   => $user['id'],
                'vote_id'   => $vote_id,
                'person_id' => $person_id,
                'gift_id'   => $gift_id,
                'name'      => $gift['name'],
                'image'     => $gift['image'],
                'money'     => 0,
                'num'       => $gift['num'],
                'order_no'  => kzvote_generate_no('G'),
                'status'    => '20',
                'nickname'  => $user['nickname'],
                'avatar'    => strstr($user['avatar'],'upload') ? $user['avatar']:''
            ]);

            if ($gift['num'] > 0){
                $person = \app\common\model\kzvote\Person::get($person_id);

                $vote->setInc('count', $gift->num);
                $person->setInc('count', $gift->num);
            }
            $this->success('赠送成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 购买礼物
     */
    public function pay()
    {
        $vote_id   = $this->request->request('vote_id');
        $person_id = $this->request->request('person_id');
        $gift_id   = $this->request->request('gift_id');

        $vote = \app\common\model\kzvote\Vote::get($vote_id);
        //活动状态不是开始、审核不是通过、截止报名时间大于当前时间
        if ($vote['status'] != '10' || $vote['check'] != '1' || $vote['start_time'] > time()) {
            $this->error('当前活动不允许送礼物');
        }

        if ($vote->end_time < time()) {
            $this->error('投票活动已结束');
        }

        $gift = GiftModel::get($gift_id);
        $user = $this->auth->getUser();

        $order = Order::create([
            'admin_id'  => $vote['admin_id'],
            'user_id'   => $user['id'],
            'vote_id'   => $vote_id,
            'person_id' => $person_id,
            'gift_id'   => $gift_id,
            'name'      => $gift['name'],
            'image'     => $gift['image'],
            'money'     => $gift['money'],
            'num'       => $gift['num'],
            'order_no'  => kzvote_generate_no('G'),
            'nickname'  => $user['nickname'],
            'avatar'    => $user['avatar']
        ]);

        if ($order) {
            $service = new WechatService();
            $result  = $service->unifyPay($this->platform,kzvote_getOpenid($user['id'],$this->platform), $order->order_no, $order->money * 100, '支付下单');
            $this->success('',$result);
        }
        $this->error('系统异常');
    }
}
